package com.shxy.rlzy.mapper;

import com.shxy.rlzy.entity.Role;
import com.shxy.rlzy.entity.UserRole;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * <p>
 *  Mapper 接口
 * </p>
 *
 * @author tanhao
 * @since 2021-08-16
 */
public interface UserRoleMapper extends BaseMapper<UserRole> {
    /**
     * 查询用户拥有的角色列表
     * @param userId
     * @return
     */
    List<Role> selectRolesOfUser(Long userId);

    /**
     * 根据用户名查询用户角色
     * @param userName
     * @return
     */
    @Select("SELECT ur.* FROM sys_user_role AS ur LEFT JOIN sys_user AS u ON ur.user_id=u.id WHERE u.username=#{userName}")
    UserRole getByUserName(String userName);

    @Select("SELECT * FROM sys_user_role WHERE user_id=#{userId}")
    UserRole getByUserId(Long userId);

    @Select("SELECT r.* FROM sys_role AS r LEFT JOIN sys_user_role AS ur ON r.id=ur.role_id WHERE ur.user_id=#{userId}")
    Role getRoleByUserId(Long userId);
}
